<?php
/*
  function: 标签模型

  create by: 徐闻洁
  create date: 2016.12.19
  description:

  modification 0.0.3
  modified by:
  description:
  2017/5/2    modify加入action
 */
namespace app\common\model;

use think\Model;

class Label extends Model {
    var $userId;
    var $unit_id;
    function __construct() {
        //获取user_id
        if (is_null($this->userId)) {
            $userId = $this->userId = model('user')->getUserId();
        } else {
            $userId = $this->userId;
        }
        if (!$userId) {
            return;
        }
    }

    public function unit($unit_id)
    {
        if(!$unit_id){
            $this->unit_id = 0;
            return;
        }
        $this->unit_id = $unit_id;
    }
    //list
    public function rank($demand) {
        $where = "state = '1'";
        if($this->unit_id){
            $where .= " and unit_id = $this->unit_id";
        }
        $result = [];
        $label = \think\Db::name($demand."_label")->where($where)
                            ->order('category,rank')->select();
        foreach ($label as $k => $v) {
            if ($v['rank'] == 0) {
                $result[$v['category']]['category_cn'] = $v['cn'];
                $result[$v['category']]['category_en'] = $v['en'];
                $result[$v['category']]['label_id'] = $v['label_id'];
//                $result[$v['category']]['category_text'] = $v['cn'] . ' ' . $v['en'];
                $result[$v['category']]['category_text'] = $v['cn'];
            } else {
                $result[$v['category']]['labels'][] = $v;
            }
        }
        foreach ($result as $key => $value) {
            if(isset($value['labels'])) {
                foreach ($value['labels'] as $key_n => $value_n) {
                    $result[$key]['labels'][$key_n]['label_en'] = $value_n['en'];
                    $result[$key]['labels'][$key_n]['label_cn'] = $value_n['cn'];
                    $result[$key]['labels'][$key_n]['label_id'] = $value_n['label_id'];
//                    $result[$key]['labels'][$key_n]['label_text'] = $value_n['cn'] . ' ' . $value_n['en'];
                    $result[$key]['labels'][$key_n]['label_text'] = $value_n['cn'];
                    unset($result[$key]['labels'][$key_n]['en'], $result[$key]['labels'][$key_n]['cn'], $result[$key]['labels'][$key_n]['state'], $result[$key]['labels'][$key_n]['rank'], $result[$key]['labels'][$key_n]['category'], $result[$key]['labels'][$key_n]['user_id']);
                }
            }
        }

        return array('state' => 1, 'data' => array('rank' => $result));
    }

    //modify
    function modify($demand, $labelinfo = null) {
        if(!$labelinfo) return array('state'=>0,'errormsg'=>'信息不能为空');
        $M = $demand . "_label";
        //判断是否是管理员操作
//        if (!model('admin')->is($demand, $ultimate = true)['state']) {
//            return array('state' => 0, 'errormsg' => 'authority incorrect');
//        }
        $info['state'] = 0;
        //首先把以前的所有label状态改为已删除
        $where = "state = 1";
        if($this->unit_id){
            $where .= " and unit_id = $this->unit_id";
        }
        \think\Db::name($M)->where($where)->update($info);
        foreach ($labelinfo as $k => $v) {
            $cn = $v['category_cn'];
            $where = "cn = '$cn'";
            if (\think\Db::name($M)->where($where)->find()) {
                if(isset($v['category_en'])) {
                    $s['en'] = $v['category_en'];
                }
                $s['category'] = $k;
                $s['rank'] = 0;
                $s['user_id'] = $this->userId;
                $s['state'] = '1';
                $saveState = \think\Db::name($M)->where($where)->update($s);
                model('action')->add('label_modify',"$cn","$v[category_cn]");
            } else {
                $data['cn'] = $v['category_cn'];
                if(isset($v['category_en'])) {
                    $data['en'] = $v['category_en'];
                }
                $data['category'] = $k;
                $data['rank'] = 0;
                $data['user_id'] = $this->userId;
                $data['state'] = 1;
                $saveState =\think\Db::name($M)->insert($data);
                model('action')->add('label_modify',"cn","$v[category_cn]");
            }
            foreach ($v['labels'] as $key => $value) {
                $cn_l = $value['label_cn'];
                $rank = $key + 1;
                $where_l = "cn = '$cn_l'";
                unset($data_l);
                $id = \think\Db::name($M)->where($where_l)->find();
                if ($id) {
                    if(isset($value['label_en'])){
                    $data_l['en'] = $value['label_en'];
                    }
                    $data_l['category'] = $k;
                    $data_l['user_id'] = $this->userId;
                    $data_l['rank'] = $rank;
                    $data_l['state'] = 1;
                    $saveState_l = \think\Db::name($M)->where($where_l)->update($data_l);
                    model('action')->add('label_modify',"cn","$cn_l");
                }else {
                    if(isset($value['label_en'])) {
                        $data_l['en'] = $value['label_en'];
                    }
                    $data_l['cn'] = $value['label_cn'];
                    $data_l['category'] = $k;
                    $data_l['user_id'] = $this->userId;
                    $data_l['rank'] = $rank;
                    $data_l['state'] = 1;
                    $saveState_l = \think\Db::name($M)->insert($data_l);
                    model('action')->add('label_modify',"cn","$value[label_cn]");
                }
            }
        }

        return array('state' => 1, 'errormsg' => 'ok');
    }

}
